<template>
  <div class="friend-list-container">
    <h3>我的好友</h3>
    <form class="add-friend-form" @submit.prevent="handleAddFriend">
      <input v-model="addFriendName" type="text" placeholder="输入用户名添加好友" maxlength="20" />
      <button type="submit">添加</button>
    </form>
    <div v-if="errorMsg" class="error-msg">{{ errorMsg }}</div>
    <ul class="friend-list">
      <li v-for="friend in friends" :key="friend">
        <span class="friend-name" @click="$emit('select-friend', friend)">{{ friend }}</span>
        <button class="delete-btn" @click="handleDeleteFriend(friend)">删除</button>
      </li>
    </ul>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue'

const props = defineProps({
  friends: {
    type: Array,
    default: () => []
  },
  username: {
    type: String,
    required: true
  }
})
const emit = defineEmits(['friend-added', 'friend-removed', 'select-friend'])

const addFriendName = ref('')
const errorMsg = ref('')

const handleAddFriend = async () => {
  errorMsg.value = ''
  const name = addFriendName.value.trim()
  if (!name) {
    errorMsg.value = '请输入好友用户名'
    return
  }
  if (name === props.username) {
    errorMsg.value = '不能添加自己为好友'
    return
  }
  try {
    const res = await fetch('http://localhost:3000/add_friend', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ username: props.username, friend: name })
    })
    const data = await res.json()
    if (!data.success) {
      errorMsg.value = data.msg || '添加失败'
      return
    }
    emit('friend-added', name)
    addFriendName.value = ''
  } catch (e) {
    errorMsg.value = '网络错误'
  }
}

const handleDeleteFriend = async (friend) => {
  errorMsg.value = ''
  try {
    const res = await fetch('http://localhost:3000/remove_friend', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ username: props.username, friend })
    })
    const data = await res.json()
    if (!data.success) {
      errorMsg.value = data.msg || '删除失败'
      return
    }
    emit('friend-removed', friend)
  } catch (e) {
    errorMsg.value = '网络错误'
  }
}
</script>

<style scoped>
.friend-list-container {
  padding: 16px 12px;
  background: #f8f9fa;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.04);
}
.friend-list-container h3 {
  margin-bottom: 10px;
  font-size: 1.1rem;
  color: #1976d2;
}
.add-friend-form {
  display: flex;
  gap: 8px;
  margin-bottom: 10px;
}
.add-friend-form input {
  flex: 1;
  padding: 6px 10px;
  border: 1px solid #e1e5e9;
  border-radius: 6px;
  font-size: 1rem;
}
.add-friend-form button {
  padding: 6px 14px;
  background: #1976d2;
  color: #fff;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 1rem;
}
.friend-list {
  list-style: none;
  padding: 0;
  margin: 0;
}
.friend-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 0;
  border-bottom: 1px solid #e1e5e9;
}
.friend-name {
  cursor: pointer;
  color: #1976d2;
  font-weight: 500;
}
.friend-name:hover {
  text-decoration: underline;
}
.delete-btn {
  background: #f44336;
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 4px 10px;
  cursor: pointer;
  font-size: 0.95rem;
}
.delete-btn:hover {
  background: #c62828;
}
.error-msg {
  color: #f44336;
  margin-bottom: 8px;
  font-size: 0.95rem;
}
</style>
